package com.findtech.threePomelos.activity;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.bluetooth.AppManager;
import com.findtech.threePomelos.fragment.BindCarButtonFragment;
import com.findtech.threePomelos.fragment.SearchFragment;
import com.findtech.threePomelos.utils.RequestUtils;

import java.util.ArrayList;


public class BindCarActivity extends MyActionBarActivity implements
        View.OnClickListener, SearchFragment.SelectBlueDevice {

	RelativeLayout bindCarLayout;
	SearchFragment searchFragment;
	BindCarButtonFragment bindCarButtonFragment;
	private ImageView searchMyCar;
	private Button skipToHomeView;
    private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_bind_car);
        bundle = getIntent().getExtras();
		initView();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
        if (bundle != null) {
            if ("MyDeviceOperateActivity".equals(bundle.getString("from"))) {
                setToolbar("三个柚子推车", true);
                skipToHomeView.setVisibility(View.GONE);
            } else {
                setToolbar("三个柚子推车", false);
            }
        } else {
            setToolbar("三个柚子推车", false);
        }
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@SuppressWarnings("static-access")
	protected void initView() {
		bindCarLayout = (RelativeLayout) findViewById(R.id.bind_car_layout);
		searchMyCar = (ImageView) findViewById(R.id.my_car);
		skipToHomeView = (Button) findViewById(R.id.skip_home_view);
		searchMyCar.setOnClickListener(this);
		skipToHomeView.setOnClickListener(this);

		searchFragment = new SearchFragment(this);
        if (bundle != null) {
            bindCarButtonFragment = new BindCarButtonFragment(this, bundle.getString("from"));
        } else {
            bindCarButtonFragment = new BindCarButtonFragment(this);
        }
		searchFragment.setSelectBlueDeviceListener(this);
		getSupportFragmentManager().beginTransaction().replace(R.id.bind_car_layout, searchFragment).commitAllowingStateLoss();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onClick(View view) {
		if (view == searchMyCar) {
			initView();
		}
		if (view == skipToHomeView) {
			Intent intent = new Intent(this, HomeActivity.class);
			intent.putExtra("isSkip", true);
			startActivity(intent);
		}
	}

	@Override
	public void selectBlueDevice(ArrayList<BluetoothDevice> arraySource, int position) {
		app.manager.bluetoothDevice = arraySource.get(position);
		RequestUtils.getSharepreferenceEditor(this).
				putString(RequestUtils.DEVICE, app.manager.bluetoothDevice.getAddress()).commit();
		getSupportFragmentManager().beginTransaction().replace(R.id.bind_car_layout, bindCarButtonFragment).commit();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (bundle != null) {
                if ("MyDeviceOperateActivity".equals(bundle.getString("from"))) {
                    finish();
                } else {
                    MyApplication.getInstance().exit();
                }
            } else {
                MyApplication.getInstance().exit();
            }
            return true;
        }
		return super.onKeyDown(keyCode, event);
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppManager.REQUEST_CODE && resultCode == RESULT_OK) {
            initView();
        }
    }


}
